package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnQxService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnbgQxService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnbgService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnyqQxService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnyqService;
import com.augurit.gzcgwsp.jzfqwczzxn.web.form.YwYnJzfqwczzXnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IXnXzService;
import com.augurit.gzcgwsp.readAndParseXML.util.ConfigFjForPf;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 消纳 新增、变更、延期（由于方欣放在一个目录下，所以也合起来在这个类里面处理。变更和延期两个类暂时是没有用到的10-30）
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class XnXzServiceImpl implements IXnXzService{
	Logger logger = (Logger) LoggerFactory.getLogger(XnXzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzXnService ywYnJzfqwczzXnService;	
	@Autowired
	private IYwYnJzfqwczzXnbgService ywYnJzfqwczzXnbgService;	
	@Autowired
	private IYwYnJzfqwczzXnyqService ywYnJzfqwczzXnyqService;
	@Autowired
	private IYwYnJzfqwczzXnQxService ywYnJzfqwczzXnQxService;	
	@Autowired
	private IYwYnJzfqwczzXnbgQxService ywYnJzfqwczzXnbgQxService;	
	@Autowired
	private IYwYnJzfqwczzXnyqQxService ywYnJzfqwczzXnyqQxService;

	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String xnXzfilepath = localResource.getString("xnXzfilepath");

	// 附件存放路径
	private final String xnXzfujianpath = localResource.getString("xnXzfujianpath");

	// 解析成功反馈xml存放路径
	private final String xnXzfeedbackPath = localResource.getString("xnXzfeedbackPath");

	// 解析异常xml存放路径
	private final String xnXzexceptionPath = localResource.getString("xnXzexceptionPath");

	// 解析后，xml备份
	private final String xnXzbackPath = localResource.getString("xnXzbackPath");

	private Map<String, String> xnXzKeyMap;// xml内容的key

	private Map<String, String> xnXzKeyMapHead;// xml头部的key

	private Map<String, Long> fujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initxnXzMap() {
		// 初始化数据部分对应的map
		xnXzKeyMap = new HashMap<String, String>();
		xnXzKeyMap.put("approvalNo", "approvalNo");
		xnXzKeyMap.put("businessCode", "businessCode");
		xnXzKeyMap.put("YSL_NO", "YSL_NO");
		xnXzKeyMap.put("sendTime", "sendTime");
		
		xnXzKeyMap.put("GCDZ", "GCDZ");
		xnXzKeyMap.put("SQDW", "SQDW");
		xnXzKeyMap.put("GCMC", "GCMC");
		xnXzKeyMap.put("JSDWDZ", "JSDWDZ");
		xnXzKeyMap.put("JSDWLXR", "JSDWLXR");
		xnXzKeyMap.put("JSDWLXRLXDH", "JSDWLXRLXDH");
		xnXzKeyMap.put("SGDWGZ", "SGDW_GZ");
		xnXzKeyMap.put("SGDWXMJL", "SGDWXMJL");
		xnXzKeyMap.put("SGDWXMJLLXDH", "SGDWXMJLLXDH");
		xnXzKeyMap.put("SGDWLXR", "SGDW_LXR");
		xnXzKeyMap.put("SGDWLXRLXDH", "SGDWLXRLXDH");
		xnXzKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		xnXzKeyMap.put("BRANCH", "BRANCH");
		

		// 初始化头部对应的map
		xnXzKeyMapHead = new HashMap<String, String>();
		xnXzKeyMapHead.put("sn", "sn");
		xnXzKeyMapHead.put("sender", "sender");
		// xnXzKeyMapHead.put("receiver", "receiver");
		xnXzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		fujianMap = new HashMap<String, Long>();
	}

	public void readXml(String projectPath) {
		String templeCode = "";
		if (xnXzKeyMap == null)
			this.initxnXzMap();
		File file = new File(xnXzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(xnXzKeyMap, filePath,xnXzfeedbackPath, xnXzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(xnXzKeyMapHead,files[i], xnXzfeedbackPath, xnXzexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzXnForm form = new YwYnJzfqwczzXnForm();
						for (String mapKey : xnXzKeyMap.keySet()) {							
							form.setYslNo(parseXMLService.getMapValue(xnXzKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(xnXzKeyMap.get("SQDW")));
							templeCode = parseXMLService.getMapValue(xnXzKeyMap.get("BRANCH"));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(xnXzKeyMap.get("SQDW")),templeCode);
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(xnXzKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(xnXzKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(xnXzKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(xnXzKeyMap.get("JSDWDZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(xnXzKeyMap.get("JSDWLXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(xnXzKeyMap.get("JSDWLXRLXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(xnXzKeyMap.get("SGDWGZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(xnXzKeyMap.get("SGDWXMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(xnXzKeyMap.get("SGDWXMJLLXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(xnXzKeyMap.get("SGDWLXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(xnXzKeyMap.get("SGDWLXRLXDH")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(xnXzKeyMap.get("SBJZFQWCZL")));

							form.setBranch(templeCode);
							form.setApprovalNo(parseXMLService.getMapValue(xnXzKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(xnXzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(xnXzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}
						
						// 调用DAO，保存到数据库
						try {
							if("jzfqwczzxn_xz".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnXzfujianMap(fujianMap);
								ywYnJzfqwczzXnService.save(form);
							}else if("jzfqwczzxn_bg".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnBgfujianMap(fujianMap);
								ywYnJzfqwczzXnbgService.save(form);
							}else if("jzfqwczzxn_yq".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnYqfujianMap(fujianMap);
								ywYnJzfqwczzXnyqService.save(form);
							}else if("jzfqwczzxnqx_xz".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnQxYqfujianMap(fujianMap);
								ywYnJzfqwczzXnQxService.save(form);
							}else if("jzfqwczzxnqx_bg".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnQxYqfujianMap(fujianMap);
								ywYnJzfqwczzXnbgQxService.save(form);
							}else if("jzfqwczzxnqx_yq".equals(templeCode)){
								fujianMap = ConfigFjForPf.getXnQxYqfujianMap(fujianMap);
								ywYnJzfqwczzXnyqQxService.save(form);
							}		
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									xnXzKeyMapHead, xnXzfeedbackPath, xnXzexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, xnXzfujianpath, xnXzfeedbackPath, xnXzexceptionPath, xnXzKeyMapHead,
								files, fujianMap, i, form.getQyid(), form.getId(), templeCode);
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								xnXzKeyMapHead, xnXzfeedbackPath, xnXzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, xnXzbackPath + fileName,fileName);
				}
			}
		}
	}
}
